/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package hhf.worldzones.daos;

import hhf.worldzones.entities.Zone;
import javax.ejb.TransactionAttribute;
import javax.ejb.TransactionAttributeType;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;
import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Root;

/**
 *
 * @author François
 */
public abstract class ZoneDAO<T extends Zone> extends AbstractDAO<T> {
	@PersistenceContext(unitName = "worldZonesPU")
	private EntityManager em;

	@Override
	protected EntityManager getEntityManager() {
		return em;
	}

	public ZoneDAO(Class<T> entityClass) {
		super(entityClass);
	}
	
	@TransactionAttribute(TransactionAttributeType.NOT_SUPPORTED)
	public boolean contains(String code) {
		CriteriaBuilder cb = getEntityManager().getCriteriaBuilder();
		CriteriaQuery cq = cb.createQuery();
		Root<T> rt = cq.from(entityClass);
		cq.select(cb.count(rt));
		cq.where(cb.equal(rt.get("code"), code));
		Query q = getEntityManager().createQuery(cq);
		return (Long) q.getSingleResult() >= 1;
	}
}
